#include <fcntl.h>
#include <unistd.h>
#include <iostream>
#include <libpmemobj++/make_persistent_atomic.hpp>
#include <libpmemobj++/p.hpp>
#include <libpmemobj++/persistent_ptr.hpp>
#include <libpmemobj++/pool.hpp>
#include <libpmemobj++/transaction.hpp>


struct compound_type {
    compound_type(int val1, double val2) : a(val1), b(val2) {
    }
    void set(int val1, double val2) {
        a = val1;
        b = val2;
    }
    pmem::obj::p<int> a;
    pmem::obj::p<double> b;
};
struct root {
    pmem::obj::persistent_ptr<compound_type> comp = nullptr;
};
pmem::obj::persistent_ptr<root> proot;

void make_persistent_atomic_example(pmem::obj::pool_base &pop) {
	pmem::obj::make_persistent_atomic<compound_type>(pop, proot->comp, 1, 2.0);
}

void show_example(pmem::obj::pool_base &pop) {
    std::cout << proot->comp->a << "---" << proot->comp->b << std::endl;
}
static inline int file_exists(char const *file) {
    return access(file, F_OK);
}

int main() {
	pmem::obj::pool<root> pop;
    const char * path = "dst_file";
    if (file_exists(path) != 0) {
        pop = pmem::obj::pool<root>::create(path, "string_example", PMEMOBJ_MIN_POOL);
    } else {
        pop = pmem::obj::pool<root>::open(path, "string_example");
    }
    proot = pop.root();
	
    make_persistent_atomic_example(pop);
    show_example(pop);


	pop.close();
	return 0;
}
